package cn.zyl.quartz.controller;

import cn.zyl.quartz.bean.JobParameter;
import cn.zyl.quartz.service.TaskService;
import cn.zyl.quartz.service.impl.TaskServiceImpl;
import cn.zyl.quartz.typeEnum.StrategyEnum;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhaoyunlong
 * @Description: Servlet测试，在网页中通过ip地址调用，创建Job
 * @Date: Create in 17:06 2018/12/21
 */
@WebServlet("/TestServlet")
public class AddServlet extends HttpServlet{

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String, String> map = new HashMap<>();
        map.put( "name", "11111");

        StrategyEnum strategyEnum = StrategyEnum.DoNothing;

        JobParameter jobParameter = new JobParameter();
        jobParameter.setJobName( "111" );
        jobParameter.setJobGroup( "group" );
        jobParameter.setTriggerName( "1112" );
        jobParameter.setTriggerGroup( "group" );
        jobParameter.setCronExpression( "0/3 * * * * ?" );
        jobParameter.setJobClass( "cn.zyl.quartz.Job.Job1" );
        jobParameter.setDescription( "test" );
        jobParameter.setStrategy( strategyEnum );
        jobParameter.setParam( map );

        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = null;
        try {
            time = df.parse("2018-12-22 10:51:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        jobParameter.setStartTime( time );

        TaskServiceImpl taskService = new TaskServiceImpl();
        taskService.addCronJob( jobParameter );

    }
}
